package com.metagurukul.metagranth.actionservlets;

import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.metagurukul.metagranth.service.QuestionService;
import com.metagurukul.metagranth.vo.Question;
import com.metagurukul.metagranth.vo.Tag;


/**
 * Servlet implementation class AskQuestionServlet
 * This servlet is responsible for handling the functionality of asking a question by the user.
 * it creates a hashmap for storing the all the credentials associated with the question posted, its description,
 * tags associated.
 * It sets all these parameters (Value Objects)
 * It also creates a session associated with each user , when he posts the question and maintains it throughout
 * till application is logged off.
 * 
 * It then calls the askQuestion() function in the Service layer that will handle the functionaliy of saving the question
 * by then interacting with the DAO classes.
 * Once the question is asked , on success the user is redirected to "home.jsp" page
 * else on "errorPage.jsp"
 * 
 * @author team MetaGranth
 *
 */
public class AskQuestionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AskQuestionServlet() {
		super();
		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPerform(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPerform(request, response);
	}

	/**
	 * this function will get the titleName, question description,tagValue of the asked question, and sets the same by value objects
	 * It will t also creates a session associated with each user , when he posts the question 
	 * and maintains it throughout till application is logged off.
	 * Once the question is asked , on success the user is redirected to "home.jsp" page
	 * else on "errorPage.jsp"
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doPerform(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		if(session.getAttribute("userId")!=null){
				try{
				Map<String, String[]> params = request.getParameterMap();
				String title = params.get("titlename")[0];
				String description = params.get("description")[0];
				String tagValue = params.get("tagnames")[0];
				int userId = (Integer) session.getAttribute("userId");
			
				Question question = new Question();
				Tag tag = new Tag();
				question.setTitle(title);
				question.setQuestion(description);
				question.setUserId(userId);
				tag.setTagName(tagValue);
				
				int status = QuestionService.askQuestion(question, tag);
				response.sendRedirect("home.jsp");
			}catch(Exception exception) {
				//exception.printStackTrace();
				request.setAttribute("errormsg", exception.getMessage());
				request.getRequestDispatcher("errorPage.jsp").forward(request, response);
			}
		}else {
			response.sendRedirect("index.jsp");
		}
	}
}
